// backend/routes/studentRoutes.js

const express = require('express');
const router = express.Router();
const Student = require('../models/Student'); // 引入数据模型

// 获取所有学生
router.get('/', async (req, res) => {
  try {
    const students = await Student.getAll();
    res.json({
      success: true,
      data: students
    });
  } catch (error) {
    console.error('获取学生数据失败:', error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

// 根据ID获取学生（可选）
router.get('/:id', async (req, res) => {
  try {
    const student = await Student.getById(req.params.id);
    if (!student) {
      return res.status(404).json({
        success: false,
        message: '学生未找到'
      });
    }
    res.json({
      success: true,
      data: student
    });
  } catch (error) {
    console.error(`获取学生ID=${req.params.id} 失败:`, error);
    res.status(500).json({
      success: false,
      message: '服务器错误',
      error: error.message
    });
  }
});

module.exports = router;